package com.ruoyi.hospital.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hospital.service.SchedulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.SchedulesMapper;
import com.ruoyi.hospital.domain.Schedules;
import org.springframework.transaction.annotation.Transactional;


/**
 * 排班信息Service业务层处理
 *
 * @author xiaozhao
 * @date 2025-07-03
 */
@Service
public class SchedulesServiceImpl implements SchedulesService
{
    @Autowired
    private SchedulesMapper schedulesMapper;

    /**
     * 查询排班信息
     *
     * @param scheduleId 排班信息主键
     * @return 排班信息
     */
    @Override
    public Schedules selectSchedulesByScheduleId(Long scheduleId)
    {
        return schedulesMapper.selectSchedulesByScheduleId(scheduleId);
    }

    /**
     * 查询排班信息列表
     *
     * @param schedules 排班信息
     * @return 排班信息
     */
    @Override
    public List<Schedules> selectSchedulesList(Schedules schedules)
    {
        return schedulesMapper.selectSchedulesList(schedules);
    }

    /**
     * 新增排班信息
     *
     * @param schedules 排班信息
     * @return 结果
     */
    @Override
    public int insertSchedules(Schedules schedules)
    {
        schedules.setCreateTime(DateUtils.getNowDate());
        return schedulesMapper.insertSchedules(schedules);
    }

    /**
     * 修改排班信息
     *
     * @param schedules 排班信息
     * @return 结果
     */

    @Override
    public int updateSchedules(Schedules schedules) {
        schedules.setUpdateTime(DateUtils.getNowDate());
        return schedulesMapper.updateSchedules(schedules);
    }

    /**
     * 批量删除排班信息
     *
     * @param scheduleIds 需要删除的排班信息主键
     * @return 结果
     */
    @Override
    public int deleteSchedulesByScheduleIds(Long[] scheduleIds)
    {
        return schedulesMapper.deleteSchedulesByScheduleIds(scheduleIds);
    }

    /**
     * 删除排班信息信息
     *
     * @param scheduleId 排班信息主键
     * @return 结果
     */
    @Override
    public int deleteSchedulesByScheduleId(Long scheduleId)
    {
        return schedulesMapper.deleteSchedulesByScheduleId(scheduleId);
    }

    @Override
    @Transactional
    public int updateQuota(Long doctorId, Date appointmentDate, String timeSlot, int change) {
        if (change < 0) {
            // 查询排班信息
            Schedules schedule = schedulesMapper.selectOne(
                    new QueryWrapper<Schedules>()
                            .eq("doctor_id", doctorId)
                            .eq("date", appointmentDate)
            );

            if (schedule != null) {
                Long currentRemaining = 0L;
                if ("上午".equals(timeSlot)) currentRemaining = schedule.getMorningRemaining();
                if ("下午".equals(timeSlot)) currentRemaining = schedule.getAfternoonRemaining();
                if ("夜间".equals(timeSlot)) currentRemaining = schedule.getNightRemaining();

                if (currentRemaining + change < 0) {
                    throw new IllegalArgumentException("号源不足，更新失败");
                }
            }
        }

        return schedulesMapper.updateQuota(doctorId, appointmentDate, timeSlot, change);
    }

    @Override
    public List<Schedules> selectSchedulesByDeptIdAndDate(Long deptId, Date appointmentDate) {
        return schedulesMapper.selectSchedulesByDeptIdAndDate(deptId, appointmentDate);
    }
}
